/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */

import React, { Component } from 'react';
import { UserInfo } from "./components/User"
import { Platform, Text, View, NativeModules, FlatList, Image, TouchableOpacity, DeviceEventEmitter,Vibration } from 'react-native';
import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';
import Home from './pages/Home';
import Chat from './pages/Chat';
import Users from './pages/Users';
import Connect from './pages/Connect';
import Login from './pages/Login';
import AxiosFn from './api';
import { insert, read } from './utils/asyncsql';
import { chartCenter } from "./components/ChatCenter";
window.servers = {
    server: "192.168.1.181",
};
window.userinfo = {
    App: ''
};
console.disableYellowBox = true;
const PATTERN = 500;
class App extends Component {
    constructor(props) {
        super(props)
        UserInfo.on('infoDone',()=>{  // 获取到本地登录信息
            this._init()
        })

        this.state = {
            open: false
        };
        DeviceEventEmitter.addListener('LoginSuccess', async () => {
            this._init();
        })

        //  处理消息流
        chartCenter.client.fillter._txt_message=async (data)=>{
            // DeviceEventEmitter.emit('onRtcMessage');
            Vibration.vibrate(PATTERN);
            DeviceEventEmitter.emit('onRtcHomeMessage')
        }

    }


    async _init() {
        //登录信息不存在
        UserInfo.LoginError = () => {
            this.props.navigation.navigate("Login");
        }

        let {id} = UserInfo

        console.log("本地用户的信息:",UserInfo,UserInfo.id);

        let state = await AxiosFn("getcontacks", { id: UserInfo.id });
        console.log("线上用户表", state.data);

        state.data.map(async(o, index) => { // 保存联系人进缓存
            return await insert("replace into users(id,sex,avatar,tabs,pname) values (?,?,?,?,?)", [o.id, o.sex, o.avatar, o.tab, o.pname]);
        })
        
        let x = await read(`select * from users`);
        console.log("数据得到",x);

    }
}

userinfo.App = new App();

let Tab = createBottomTabNavigator({
    Home: {
        screen: Home,
        navigationOptions: ({ navigation }) => ({
            title: "聊天",
            swipeEnabled:true,
            tabBarIcon: ({ focused }) => ( <
                Image resizeMode = 'contain'
                source = { focused ? require('./public/chat.png') : require('./public/chat.png') } style = { { width: 20, height: 20 } }
                />
            )
        })
    },
    Connect: {
        screen: Connect,
        navigationOptions: ({ navigation }) => ({
            title: "好友",
            swipeEnabled:true,
            tabBarIcon: ({ focused }) => ( <
                Image resizeMode = 'contain'
                source = { focused ? require('./public/lxr.png') : require('./public/lxr.png') } style = { { width: 20, height: 20 } }
                />
            )
        })
    },
    Users: {
        screen: Users,
        navigationOptions: ({ navigation }) => ({
            title: "我的",
            tabBarIcon: ({ focused }) => ( <
                Image resizeMode = 'contain'
                source = { focused ? require('./public/my.png') : require('./public/my.png') } style = { { width: 20, height: 20 } }
                />
            )
        })
    }
}, {
    swipeEnabled: true,
    initialRouteName: "Home",
    tabBarOptions: {
        showLabel: true,
        allowFontScaling: true,
        activeBackgroundColor: "#53afff",
        labelStyle: {
            fontSize: 12,
            color: "black"
        },
        style: {
            backgroundColor: '#fff',
        },
    }
});

export default createStackNavigator({
    Tab: {
        screen: Tab
    },
    Chat: {
        screen: Chat
    },
    Home: {
        screen: Home,
    },
    Users: {
        screen: Users,
    },
    Login: {
        screen: Login
    },
}, {
    swipeEnabled: true,
    initialRouteName: "Tab",
    navigationOptions: { // 屏幕导航的默认选项, 也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)

        headerStyle: {
            backgroundColor: '#fff',
            display: "flex",
            color: '#349aff'
        },
        headerTintColor: 'black'
    },
});
